//  AppDelegate.h
//  AnimatTabbarSample
//
//  Created by chenyanming on 14-4-9.
//  Copyright (c) 2014年 chenyanming. All rights reserved.
//

#import "lvfjAnimateTabbar.h"


@implementation lvfjAnimateTabbar
@synthesize  firstBtn,secondBtn,thirdBtn,fourthBtn,fifthBtn,delegate;

enum barsize{
    
    tabitem_hight = 55,
    
    tab_hight=55,
    other_offtop=20,
    
    img_hight=25,
    img_width=25,
    
    img_y = 8
};

- (instancetype)initWithFrame:(CGRect)frame
                     imgArray:(NSArray *)imgArray{
    CGRect frame1=CGRectMake(frame.origin.x, frame.size.height-75, frame.size.width, 75);
    self = [super initWithFrame:frame1];
    
    if (self) {
        
        CGFloat tabitem_width = frame.size.width / 5;
        CGFloat img_x = (tabitem_width - img_width) / 2;
        self.backgroundColor = [UIColor clearColor];
        
        UIView *bakcView = [[UIView alloc]initWithFrame:CGRectMake(0, 20, frame.size.width, 55)];
        bakcView.backgroundColor = [[UIColor alloc]initWithWhite:1 alpha:0.95];;
        [self addSubview:bakcView];
        
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 20, frame.size.width, 0.5)];
        lineView.backgroundColor = [UIColor lightGrayColor];
        [self addSubview:lineView];
        
//        backBtn=[UIButton buttonWithType:UIButtonTypeCustom];
//        [backBtn setFrame:CGRectMake(0, 0, frame.size.width, tab_hight)];
//        [backBtn setBackgroundImage:[UIImage imageNamed:@"tabBar_back"] forState:UIControlStateNormal];
//        [backBtn setBackgroundImage:[UIImage imageNamed:@"tabBar_back"] forState:UIControlStateSelected];
        
//        shadeBtn=[UIButton buttonWithType:UIButtonTypeCustom];
//        [shadeBtn setFrame:CGRectMake(0, other_offtop, tabitem_width, tabitem_hight)];
//        [shadeBtn setBackgroundImage:[UIImage imageNamed:@"toolBar_shade"] forState:UIControlStateNormal];
//        [shadeBtn setBackgroundImage:[UIImage imageNamed:@"toolBar_shade"] forState:UIControlStateSelected];
        
        UIImageView *btnImgView;
        UILabel *titleLb;
        
        //first
        firstBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        [firstBtn setFrame:CGRectMake(0, other_offtop, tabitem_width, tabitem_hight)];
        [firstBtn setTag:1];
        [firstBtn addTarget:self action:@selector(buttonClickAction:) forControlEvents:UIControlEventTouchUpInside];
        
        btnImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imgArray[0]]
                                       highlightedImage:[UIImage imageNamed:imgArray[1]]];
        btnImgView.frame = CGRectMake(img_x, img_y, img_width, img_hight);
        [firstBtn addSubview:btnImgView];
        
        titleLb = [[UILabel alloc]initWithFrame:CGRectMake(0, 37, tabitem_width, 13)];
        titleLb.text = @"交易";
        titleLb.textAlignment = NSTextAlignmentCenter;
        titleLb.font = [UIFont systemFontOfSize:10];
        titleLb.textColor = [UIColor lightGrayColor];
        titleLb.highlightedTextColor = [UIColor redColor];
        [firstBtn addSubview:titleLb];
        
        //second
        btnImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imgArray[2]]
                                       highlightedImage:[UIImage imageNamed:imgArray[3]]];
        btnImgView.frame = CGRectMake(img_x, img_y, img_width, img_hight);
        secondBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        [secondBtn setFrame:CGRectMake(tabitem_width, other_offtop, tabitem_width, tabitem_hight)];
        [secondBtn setTag:2];
        [secondBtn addTarget:self action:@selector(buttonClickAction:) forControlEvents:UIControlEventTouchUpInside];
        [secondBtn addSubview:btnImgView];
        
        titleLb = [[UILabel alloc]initWithFrame:CGRectMake(0, 37, tabitem_width, 13)];
        titleLb.text = @"发现";
        titleLb.textAlignment = NSTextAlignmentCenter;
        titleLb.font = [UIFont systemFontOfSize:10];
        titleLb.textColor = [UIColor lightGrayColor];
        titleLb.highlightedTextColor = [UIColor redColor];
        [secondBtn addSubview:titleLb];
        
        //third
        btnImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imgArray[4]]
                                       highlightedImage:[UIImage imageNamed:imgArray[5]]];
        btnImgView.frame = CGRectMake((tabitem_width - 60) / 2, 0, 60, 54);
        NSLog(@"%f,%f",btnImgView.frame.size.width,btnImgView.frame.size.height);
        thirdBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        [thirdBtn setFrame:CGRectMake(tabitem_width*2, 0, tabitem_width, tabitem_hight)];
        [thirdBtn setTag:3];
        [thirdBtn addTarget:self action:@selector(buttonClickAction:) forControlEvents:UIControlEventTouchUpInside];
        [thirdBtn addSubview:btnImgView];
        
        titleLb = [[UILabel alloc]initWithFrame:CGRectMake(0, 57, tabitem_width, 13)];
        titleLb.text = @"赛牛会";
        titleLb.textAlignment = NSTextAlignmentCenter;
        titleLb.font = [UIFont systemFontOfSize:10];
        titleLb.textColor = [UIColor lightGrayColor];
        titleLb.highlightedTextColor = [UIColor redColor];
        [thirdBtn addSubview:titleLb];
        
        //fourth
        btnImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imgArray[6]]
                                       highlightedImage:[UIImage imageNamed:imgArray[7]]];
        btnImgView.frame = CGRectMake(img_x, img_y, img_width, img_hight);
        fourthBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        [fourthBtn setFrame:CGRectMake(tabitem_width*3, other_offtop, tabitem_width, tabitem_hight)];
        [fourthBtn setTag:4];
        [fourthBtn addTarget:self action:@selector(buttonClickAction:) forControlEvents:UIControlEventTouchUpInside];
        [fourthBtn addSubview:btnImgView];
        
        titleLb = [[UILabel alloc]initWithFrame:CGRectMake(0, 37, tabitem_width, 13)];
        titleLb.text = @"牛圈";
        titleLb.textAlignment = NSTextAlignmentCenter;
        titleLb.font = [UIFont systemFontOfSize:10];
        titleLb.textColor = [UIColor lightGrayColor];
        titleLb.highlightedTextColor = [UIColor redColor];
        [fourthBtn addSubview:titleLb];
        
        //fifthBtn
        btnImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tabbar_personal"]
                                       highlightedImage:[UIImage imageNamed:@"tabbar_personal_selected"]];
        btnImgView.frame = CGRectMake(img_x, img_y, img_width, img_hight);
        fifthBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        [fifthBtn setFrame:CGRectMake(tabitem_width*4, other_offtop, tabitem_width, tabitem_hight)];
        [fifthBtn setTag:5];
        [fifthBtn addTarget:self action:@selector(buttonClickAction:) forControlEvents:UIControlEventTouchUpInside];
        [fifthBtn addSubview:btnImgView];
        
        titleLb = [[UILabel alloc]initWithFrame:CGRectMake(0, 37, tabitem_width, 13)];
        titleLb.text = @"个人";
        titleLb.textAlignment = NSTextAlignmentCenter;
        titleLb.font = [UIFont systemFontOfSize:10];
        titleLb.textColor = [UIColor lightGrayColor];
        titleLb.highlightedTextColor = [UIColor redColor];
        [fifthBtn addSubview:titleLb];
        
        [self addSubview:firstBtn];
        [self addSubview:secondBtn];
        [self addSubview:thirdBtn];
        [self addSubview:fourthBtn];
        [self addSubview:fifthBtn];
        
//        [backBtn addSubview:shadeBtn];
//        
//        [backBtn addSubview:firstBtn];
//        [backBtn addSubview:secondBtn];
//        [backBtn addSubview:thirdBtn];
//        [backBtn addSubview:fourthBtn];
//        [backBtn addSubview:fifthBtn];
//
//        [self addSubview:backBtn];
    }
    
  
    return self;
    
    
}

-(void)callButtonAction:(UIButton *)bt{
    switch (bt.tag) {
        case 1:
            [self.delegate firstBtnClick:bt];
            break;
            
        case 2:
            [self.delegate secondBtnClick:bt];
            break;
            
        case 3:
            [self.delegate thirdBtnClick:bt];
            break;
            
        case 4:
            [self.delegate fourthBtnClick:bt];
            break;
            
        case 5:
            [self.delegate fifthBtnClick:bt];
            break;
            
        default:
            break;
    }
}

NSInteger g_selectedTag=0;
-(void)buttonClickAction:(UIButton *)sender{
    UIButton *btn=(UIButton *)sender;
   // UIImageView *view=btn1.subviews[0];
    if(g_selectedTag==btn.tag)
        return;
    else
        g_selectedTag=btn.tag;
    
    if (firstBtn.tag!=btn.tag) {
        ((UIImageView *)firstBtn.subviews[0]).highlighted=NO;
        ((UILabel *)firstBtn.subviews[1]).highlighted=NO;
    }
    if (secondBtn.tag!=btn.tag) {
        ((UIImageView *)secondBtn.subviews[0]).highlighted=NO;
        ((UILabel *)secondBtn.subviews[1]).highlighted=NO;
    }
    if (thirdBtn.tag!=btn.tag) {
        ((UIImageView *)thirdBtn.subviews[0]).highlighted=NO;
        ((UILabel *)thirdBtn.subviews[1]).highlighted=NO;
    }
    if (fourthBtn.tag!=btn.tag) {
        ((UIImageView *)fourthBtn.subviews[0]).highlighted=NO;
        ((UILabel *)fourthBtn.subviews[1]).highlighted=NO;
    }
    if (fifthBtn.tag != btn.tag){
        ((UIImageView *)fifthBtn.subviews[0]).highlighted=NO;
        ((UILabel *)fifthBtn.subviews[1]).highlighted=NO;
    }
    
//    [self moveShadeBtn:btn];
    [self imgAnimate:btn];
    
    ((UIImageView *)btn.subviews[0]).highlighted=YES;
    ((UIImageView *)btn.subviews[1]).highlighted=YES;
    
    [self callButtonAction:btn];
    
    return;
}

-(void)jumpIndex:(NSInteger)index{
    UIButton *bt;
    switch (index) {
        case 0:
            bt = firstBtn;
            break;
            
        case 1:
            bt = secondBtn;
            break;
            
        case 2:
            bt = thirdBtn;
            break;
            
        case 3:
            bt = fourthBtn;
            break;
            
        case 4:
            bt = fifthBtn;
            break;
            
        default:
            break;
    }
    [self buttonClickAction:bt];
}

//- (void)moveShadeBtn:(UIButton*)btn{
//    
//    [UIView animateWithDuration:0.3 animations:
//     ^(void){
//         CGRect frame = shadeBtn.frame;
//         frame.origin.x = btn.frame.origin.x;
//        shadeBtn.frame = frame;
//     } completion:^(BOOL finished){//do other thing
//     }];
//}

- (void)imgAnimate:(UIButton*)btn{
//    @try {
//        UIView *view=btn.subviews[0];
//        UIView *view1=btn.subviews[1];
//    }
//    @catch (NSException *exception) {
//        NSLog(@"%@,%@,%@",exception.name,exception.reason,exception.userInfo);
//    }
//    @finally {
//        
//    }
    UIView *view=btn.subviews[0];
//    UIView *view1=btn.subviews[1];
    
    [UIView animateWithDuration:0.1 animations:
     ^(void){
         view.transform = CGAffineTransformScale(CGAffineTransformIdentity,0.5, 0.5);
//         view1.transform = CGAffineTransformScale(CGAffineTransformIdentity,0.5, 0.5);
     } completion:^(BOOL finished){//do other thing
         
         [UIView animateWithDuration:0.2 animations:
          ^(void){
              view.transform = CGAffineTransformScale(CGAffineTransformIdentity,1.2, 1.2);
//              view1.transform = CGAffineTransformScale(CGAffineTransformIdentity,1.2, 1.2);
          } completion:^(BOOL finished){//do other thing
              
              [UIView animateWithDuration:0.1 animations:
               ^(void){
                   view.transform = CGAffineTransformScale(CGAffineTransformIdentity,1,1);
//                   view1.transform = CGAffineTransformScale(CGAffineTransformIdentity,1,1);
               } completion:^(BOOL finished){//do other thing
               }];
          }];
     }];
}



@end
